// 更新分类数据以匹配前端页面实际使用的分类
const { userModel, productModel, hospitalModel, doctorModel, bannerModel } = require('./db/models/cosmetic');

async function updateFrontendCategories() {
  try {
    console.log('🔧 开始更新分类数据以匹配前端页面实际使用的分类...\n');

    // 1. 更新商品分类 - 使用前端实际使用的分类
    console.log('🛍️ 更新商品分类...');
    const productCategories = [
      '热门', '去黑眼圈', '塑形', '瘦脸', '补水保湿'
    ];
    
    const products = await productModel.find({});
    for (const product of products) {
      const randomCategory = productCategories[Math.floor(Math.random() * productCategories.length)];
      await productModel.updateOne(
        { _id: product._id },
        { $set: { category: randomCategory } }
      );
      console.log(`✅ 已更新商品 ${product.title} 分类为 ${randomCategory}`);
    }

    // 2. 更新医生分类 - 使用前端实际使用的分类
    console.log('\n👨‍⚕️ 更新医生分类...');
    const doctorCategories = [
      '热门', '瘦脸轮廓', '眼部整形', '美体塑形', '脂肪填充'
    ];
    
    const doctors = await doctorModel.find({});
    for (const doctor of doctors) {
      const randomCategory = doctorCategories[Math.floor(Math.random() * doctorCategories.length)];
      await doctorModel.updateOne(
        { _id: doctor._id },
        { $set: { category: randomCategory } }
      );
      console.log(`✅ 已更新医生 ${doctor.name} 分类为 ${randomCategory}`);
    }

    // 3. 更新医院分类 - 使用前端实际使用的分类
    console.log('\n🏥 更新医院分类...');
    const hospitalCategories = [
      '热门', '民营', '公立'
    ];
    
    const hospitals = await hospitalModel.find({});
    for (const hospital of hospitals) {
      const randomCategory = hospitalCategories[Math.floor(Math.random() * hospitalCategories.length)];
      await hospitalModel.updateOne(
        { _id: hospital._id },
        { $set: { category: randomCategory } }
      );
      console.log(`✅ 已更新医院 ${hospital.name} 分类为 ${randomCategory}`);
    }

    // 4. 验证更新结果
    console.log('\n🔍 验证分类更新结果:');
    
    const updatedProducts = await productModel.find({});
    const productCategoryCount = {};
    updatedProducts.forEach(product => {
      productCategoryCount[product.category] = (productCategoryCount[product.category] || 0) + 1;
    });
    console.log('商品分类分布:', productCategoryCount);
    
    const updatedDoctors = await doctorModel.find({});
    const doctorCategoryCount = {};
    updatedDoctors.forEach(doctor => {
      doctorCategoryCount[doctor.category] = (doctorCategoryCount[doctor.category] || 0) + 1;
    });
    console.log('医生分类分布:', doctorCategoryCount);
    
    const updatedHospitals = await hospitalModel.find({});
    const hospitalCategoryCount = {};
    updatedHospitals.forEach(hospital => {
      hospitalCategoryCount[hospital.category] = (hospitalCategoryCount[hospital.category] || 0) + 1;
    });
    console.log('医院分类分布:', hospitalCategoryCount);

    console.log('\n🎉 分类更新完成！现在所有数据都匹配前端页面实际使用的分类系统。');

  } catch (error) {
    console.error('❌ 更新分类时出错:', error);
  }
}

updateFrontendCategories();
